package com.zodiac.app.datathinking.ipproxy;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * http://www.data5u.com/
 */
public class Data5uIpProxyExtractor implements IpProxyExtractor {
    @Override
    public List<IpProxy> extract(String url) throws Exception {
        final Element body = Jsoup.connect(url).get().body();
        final List<String> thead = body.select(".wlist ul.l1 li").eachText();
        final Elements rows = body.select(".wlist ul.l2");

        List<List<String>> tbody = rows.stream().map(row -> row.select("li").eachText()).collect(Collectors.toList());
        return tbody.stream().map(item -> {
            SimpleIpProxy proxy = new SimpleIpProxy();
            proxy.setOrigin(url);
            proxy.setIp(item.get(0).trim());
            proxy.setPort(Integer.valueOf(item.get(1).trim()));
            proxy.setAnonymityLevel(item.get(2).trim());
            proxy.setArea(item.get(4).trim()+"-"+item.get(5).trim());
            proxy.setResponseTimes(Integer.valueOf(item.get(7).replaceAll("[^\\d]+", "")));

            Map<String, Object> details = new TreeMap<>();
            for (int i = 0; i < item.size(); i++) {
                details.put(thead.get(i), item.get(i));
                proxy.setDetails(details);
            }

            return proxy;
        }).collect(Collectors.toList());
    }
}

